home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-desktop-9.10-i386-PL.iso / casper / filesystem.squashfs / usr / lib / python2.6 / sets.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2009-11-11  |  18KB  |  568 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. """Classes to represent arbitrary sets (including sets of sets).
  5.  
  6. This module implements sets using dictionaries whose values are
  7. ignored.  The usual operations (union, intersection, deletion, etc.)
  8. are provided as both methods and operators.
  9.  
  10. Important: sets are not sequences!  While they support 'x in s',
  11. 'len(s)', and 'for x in s', none of those operations are unique for
  12. sequences; for example, mappings support all three as well.  The
  13. characteristic operation for sequences is subscripting with small
  14. integers: s[i], for i in range(len(s)).  Sets don't support
  15. subscripting at all.  Also, sequences allow multiple occurrences and
  16. their elements have a definite order; sets on the other hand don't
  17. record multiple occurrences and don't remember the order of element
  18. insertion (which is why they don't support s[i]).
  19.  
  20. The following classes are provided:
  21.  
  22. BaseSet -- All the operations common to both mutable and immutable
  23.     sets. This is an abstract class, not meant to be directly
  24.     instantiated.
  25.  
  26. Set -- Mutable sets, subclass of BaseSet; not hashable.
  27.  
  28. ImmutableSet -- Immutable sets, subclass of BaseSet; hashable.
  29.     An iterable argument is mandatory to create an ImmutableSet.
  30.  
  31. _TemporarilyImmutableSet -- A wrapper around a Set, hashable,
  32.     giving the same hash value as the immutable set equivalent
  33.     would have.  Do not use this class directly.
  34.  
  35. Only hashable objects can be added to a Set. In particular, you cannot
  36. really add a Set as an element to another Set; if you try, what is
  37. actually added is an ImmutableSet built from it (it compares equal to
  38. the one you tried adding).
  39.  
  40. When you ask if `x in y' where x is a Set and y is a Set or
  41. ImmutableSet, x is wrapped into a _TemporarilyImmutableSet z, and
  42. what's tested is actually `z in y'.
  43.  
  44. """
  45. from __future__ import generators
  46.  
  47. try:
  48.     from itertools import ifilter, ifilterfalse
  49. except ImportError:
  50.     
  51.     def ifilter(predicate, iterable):
  52.         if predicate is None:
  53.             
  54.             def predicate(x):
  55.                 return x
  56.  
  57.         
  58.         for x in iterable:
  59.             if predicate(x):
  60.                 yield x
  61.                 continue
  62.         
  63.  
  64.     
  65.     def ifilterfalse(predicate, iterable):
  66.         if predicate is None:
  67.             
  68.             def predicate(x):
  69.                 return x
  70.  
  71.         
  72.         for x in iterable:
  73.             if not predicate(x):
  74.                 yield x
  75.                 continue
  76.         
  77.  
  78.     
  79.     try:
  80.         (True, False)
  81.     except NameError:
  82.         (True, False) = (0 == 0, 0 != 0)
  83.  
  84.  
  85. __all__ = [
  86.     'BaseSet',
  87.     'Set',
  88.     'ImmutableSet']
  89. import warnings
  90. warnings.warn('the sets module is deprecated', DeprecationWarning, stacklevel = 2)
  91.  
  92. class BaseSet(object):
  93.     '''Common base class for mutable and immutable sets.'''
  94.     __slots__ = [
  95.         '_data']
  96.     
  97.     def __init__(self):
  98.         '''This is an abstract class.'''
  99.         if self.__class__ is BaseSet:
  100.             raise TypeError, 'BaseSet is an abstract class.  Use Set or ImmutableSet.'
  101.         self.__class__ is BaseSet
  102.  
  103.     
  104.     def __len__(self):
  105.         '''Return the number of elements of a set.'''
  106.         return len(self._data)
  107.  
  108.     
  109.     def __repr__(self):
  110.         """Return string representation of a set.
  111.  
  112.         This looks like 'Set([<list of elements>])'.
  113.         """
  114.         return self._repr()
  115.  
  116.     __str__ = __repr__
  117.     
  118.     def _repr(self, sorted = False):
  119.         elements = self._data.keys()
  120.         if sorted:
  121.             elements.sort()
  122.         
  123.         return '%s(%r)' % (self.__class__.__name__, elements)
  124.  
  125.     
  126.     def __iter__(self):
  127.         '''Return an iterator over the elements or a set.
  128.  
  129.         This is the keys iterator for the underlying dict.
  130.         '''
  131.         return self._data.iterkeys()
  132.  
  133.     
  134.     def __cmp__(self, other):
  135.         raise TypeError, "can't compare sets using cmp()"
  136.  
  137.     
  138.     def __eq__(self, other):
  139.         if isinstance(other, BaseSet):
  140.             return self._data == other._data
  141.         return False
  142.  
  143.     
  144.     def __ne__(self, other):
  145.         if isinstance(other, BaseSet):
  146.             return self._data != other._data
  147.         return True
  148.  
  149.     
  150.     def copy(self):
  151.         '''Return a shallow copy of a set.'''
  152.         result = self.__class__()
  153.         result._data.update(self._data)
  154.         return result
  155.  
  156.     __copy__ = copy
  157.     
  158.     def __deepcopy__(self, memo):
  159.         '''Return a deep copy of a set; used by copy module.'''
  160.         deepcopy = deepcopy
  161.         import copy
  162.         result = self.__class__()
  163.         memo[id(self)] = result
  164.         data = result._data
  165.         value = True
  166.         for elt in self:
  167.             data[deepcopy(elt, memo)] = value
  168.         
  169.         return result
  170.  
  171.     
  172.     def __or__(self, other):
  173.         '''Return the union of two sets as a new set.
  174.  
  175.         (I.e. all elements that are in either set.)
  176.         '''
  177.         if not isinstance(other, BaseSet):
  178.             return NotImplemented
  179.         return self.union(other)
  180.  
  181.     
  182.     def union(self, other):
  183.         '''Return the union of two sets as a new set.
  184.  
  185.         (I.e. all elements that are in either set.)
  186.         '''
  187.         result = self.__class__(self)
  188.         result._update(other)
  189.         return result
  190.  
  191.     
  192.     def __and__(self, other):
  193.         '''Return the intersection of two sets as a new set.
  194.  
  195.         (I.e. all elements that are in both sets.)
  196.         '''
  197.         if not isinstance(other, BaseSet):
  198.             return NotImplemented
  199.         return self.intersection(other)
  200.  
  201.     
  202.     def intersection(self, other):
  203.         '''Return the intersection of two sets as a new set.
  204.  
  205.         (I.e. all elements that are in both sets.)
  206.         '''
  207.         if not isinstance(other, BaseSet):
  208.             other = Set(other)
  209.         
  210.         if len(self) <= len(other):
  211.             little = self
  212.             big = other
  213.         else:
  214.             little = other
  215.             big = self
  216.         common = ifilter(big._data.has_key, little)
  217.         return self.__class__(common)
  218.  
  219.     
  220.     def __xor__(self, other):
  221.         '''Return the symmetric difference of two sets as a new set.
  222.  
  223.         (I.e. all elements that are in exactly one of the sets.)
  224.         '''
  225.         if not isinstance(other, BaseSet):
  226.             return NotImplemented
  227.         return self.symmetric_difference(other)
  228.  
  229.     
  230.     def symmetric_difference(self, other):
  231.         '''Return the symmetric difference of two sets as a new set.
  232.  
  233.         (I.e. all elements that are in exactly one of the sets.)
  234.         '''
  235.         result = self.__class__()
  236.         data = result._data
  237.         value = True
  238.         selfdata = self._data
  239.         
  240.         try:
  241.             otherdata = other._data
  242.         except AttributeError:
  243.             otherdata = Set(other)._data
  244.  
  245.         for elt in ifilterfalse(otherdata.has_key, selfdata):
  246.             data[elt] = value
  247.         
  248.         for elt in ifilterfalse(selfdata.has_key, otherdata):
  249.             data[elt] = value
  250.         
  251.         return result
  252.  
  253.     
  254.     def __sub__(self, other):
  255.         '''Return the difference of two sets as a new Set.
  256.  
  257.         (I.e. all elements that are in this set and not in the other.)
  258.         '''
  259.         if not isinstance(other, BaseSet):
  260.             return NotImplemented
  261.         return self.difference(other)
  262.  
  263.     
  264.     def difference(self, other):
  265.         '''Return the difference of two sets as a new Set.
  266.  
  267.         (I.e. all elements that are in this set and not in the other.)
  268.         '''
  269.         result = self.__class__()
  270.         data = result._data
  271.         
  272.         try:
  273.             otherdata = other._data
  274.         except AttributeError:
  275.             otherdata = Set(other)._data
  276.  
  277.         value = True
  278.         for elt in ifilterfalse(otherdata.has_key, self):
  279.             data[elt] = value
  280.         
  281.         return result
  282.  
  283.     
  284.     def __contains__(self, element):
  285.         """Report whether an element is a member of a set.
  286.  
  287.         (Called in response to the expression `element in self'.)
  288.         """
  289.         
  290.         try:
  291.             return element in self._data
  292.         except TypeError:
  293.             transform = getattr(element, '__as_temporarily_immutable__', None)
  294.             if transform is None:
  295.                 raise 
  296.             transform is None
  297.             return transform() in self._data
  298.  
  299.  
  300.     
  301.     def issubset(self, other):
  302.         '''Report whether another set contains this set.'''
  303.         self._binary_sanity_check(other)
  304.         if len(self) > len(other):
  305.             return False
  306.         for elt in ifilterfalse(other._data.has_key, self):
  307.             return False
  308.         
  309.         return True
  310.  
  311.     
  312.     def issuperset(self, other):
  313.         '''Report whether this set contains another set.'''
  314.         self._binary_sanity_check(other)
  315.         if len(self) < len(other):
  316.             return False
  317.         for elt in ifilterfalse(self._data.has_key, other):
  318.             return False
  319.         
  320.         return True
  321.  
  322.     __le__ = issubset
  323.     __ge__ = issuperset
  324.     
  325.     def __lt__(self, other):
  326.         self._binary_sanity_check(other)
  327.         if len(self) < len(other):
  328.             pass
  329.         return self.issubset(other)
  330.  
  331.     
  332.     def __gt__(self, other):
  333.         self._binary_sanity_check(other)
  334.         if len(self) > len(other):
  335.             pass
  336.         return self.issuperset(other)
  337.  
  338.     
  339.     def _binary_sanity_check(self, other):
  340.         if not isinstance(other, BaseSet):
  341.             raise TypeError, 'Binary operation only permitted between sets'
  342.         isinstance(other, BaseSet)
  343.  
  344.     
  345.     def _compute_hash(self):
  346.         result = 0
  347.         for elt in self:
  348.             result ^= hash(elt)
  349.         
  350.         return result
  351.  
  352.     
  353.     def _update(self, iterable):
  354.         data = self._data
  355.         if isinstance(iterable, BaseSet):
  356.             data.update(iterable._data)
  357.             return None
  358.         value = True
  359.  
  360.  
  361.  
  362. class ImmutableSet(BaseSet):
  363.     '''Immutable set class.'''
  364.     __slots__ = [
  365.         '_hashcode']
  366.     
  367.     def __init__(self, iterable = None):
  368.         '''Construct an immutable set from an optional iterable.'''
  369.         self._hashcode = None
  370.         self._data = { }
  371.         if iterable is not None:
  372.             self._update(iterable)
  373.         
  374.  
  375.     
  376.     def __hash__(self):
  377.         if self._hashcode is None:
  378.             self._hashcode = self._compute_hash()
  379.         
  380.         return self._hashcode
  381.  
  382.     
  383.     def __getstate__(self):
  384.         return (self._data, self._hashcode)
  385.  
  386.     
  387.     def __setstate__(self, state):
  388.         (self._data, self._hashcode) = state
  389.  
  390.  
  391.  
  392. class Set(BaseSet):
  393.     ''' Mutable set class.'''
  394.     __slots__ = []
  395.     
  396.     def __init__(self, iterable = None):
  397.         '''Construct a set from an optional iterable.'''
  398.         self._data = { }
  399.         if iterable is not None:
  400.             self._update(iterable)
  401.         
  402.  
  403.     
  404.     def __getstate__(self):
  405.         return (self._data,)
  406.  
  407.     
  408.     def __setstate__(self, data):
  409.         (self._data,) = data
  410.  
  411.     __hash__ = None
  412.     
  413.     def __ior__(self, other):
  414.         '''Update a set with the union of itself and another.'''
  415.         self._binary_sanity_check(other)
  416.         self._data.update(other._data)
  417.         return self
  418.  
  419.     
  420.     def union_update(self, other):
  421.         '''Update a set with the union of itself and another.'''
  422.         self._update(other)
  423.  
  424.     
  425.     def __iand__(self, other):
  426.         '''Update a set with the intersection of itself and another.'''
  427.         self._binary_sanity_check(other)
  428.         self._data = (self & other)._data
  429.         return self
  430.  
  431.     
  432.     def intersection_update(self, other):
  433.         '''Update a set with the intersection of itself and another.'''
  434.         if isinstance(other, BaseSet):
  435.             self &= other
  436.         else:
  437.             self._data = self.intersection(other)._data
  438.  
  439.     
  440.     def __ixor__(self, other):
  441.         '''Update a set with the symmetric difference of itself and another.'''
  442.         self._binary_sanity_check(other)
  443.         self.symmetric_difference_update(other)
  444.         return self
  445.  
  446.     
  447.     def symmetric_difference_update(self, other):
  448.         '''Update a set with the symmetric difference of itself and another.'''
  449.         data = self._data
  450.         value = True
  451.         if not isinstance(other, BaseSet):
  452.             other = Set(other)
  453.         
  454.         if self is other:
  455.             self.clear()
  456.         
  457.         for elt in other:
  458.             if elt in data:
  459.                 del data[elt]
  460.                 continue
  461.             data[elt] = value
  462.         
  463.  
  464.     
  465.     def __isub__(self, other):
  466.         '''Remove all elements of another set from this set.'''
  467.         self._binary_sanity_check(other)
  468.         self.difference_update(other)
  469.         return self
  470.  
  471.     
  472.     def difference_update(self, other):
  473.         '''Remove all elements of another set from this set.'''
  474.         data = self._data
  475.         if not isinstance(other, BaseSet):
  476.             other = Set(other)
  477.         
  478.         if self is other:
  479.             self.clear()
  480.         
  481.         for elt in ifilter(data.has_key, other):
  482.             del data[elt]
  483.         
  484.  
  485.     
  486.     def update(self, iterable):
  487.         '''Add all values from an iterable (such as a list or file).'''
  488.         self._update(iterable)
  489.  
  490.     
  491.     def clear(self):
  492.         '''Remove all elements from this set.'''
  493.         self._data.clear()
  494.  
  495.     
  496.     def add(self, element):
  497.         '''Add an element to a set.
  498.  
  499.         This has no effect if the element is already present.
  500.         '''
  501.         
  502.         try:
  503.             self._data[element] = True
  504.         except TypeError:
  505.             transform = getattr(element, '__as_immutable__', None)
  506.             if transform is None:
  507.                 raise 
  508.             transform is None
  509.             self._data[transform()] = True
  510.  
  511.  
  512.     
  513.     def remove(self, element):
  514.         '''Remove an element from a set; it must be a member.
  515.  
  516.         If the element is not a member, raise a KeyError.
  517.         '''
  518.         
  519.         try:
  520.             del self._data[element]
  521.         except TypeError:
  522.             transform = getattr(element, '__as_temporarily_immutable__', None)
  523.             if transform is None:
  524.                 raise 
  525.             transform is None
  526.             del self._data[transform()]
  527.  
  528.  
  529.     
  530.     def discard(self, element):
  531.         '''Remove an element from a set if it is a member.
  532.  
  533.         If the element is not a member, do nothing.
  534.         '''
  535.         
  536.         try:
  537.             self.remove(element)
  538.         except KeyError:
  539.             pass
  540.  
  541.  
  542.     
  543.     def pop(self):
  544.         '''Remove and return an arbitrary set element.'''
  545.         return self._data.popitem()[0]
  546.  
  547.     
  548.     def __as_immutable__(self):
  549.         return ImmutableSet(self)
  550.  
  551.     
  552.     def __as_temporarily_immutable__(self):
  553.         return _TemporarilyImmutableSet(self)
  554.  
  555.  
  556.  
  557. class _TemporarilyImmutableSet(BaseSet):
  558.     
  559.     def __init__(self, set):
  560.         self._set = set
  561.         self._data = set._data
  562.  
  563.     
  564.     def __hash__(self):
  565.         return self._set._compute_hash()
  566.  
  567.  
  568.